const {
  DataTypes
} = require('sequelize');

const DateUtils = require('../utils/dateUtils');
module.exports = sequelize => {
  const attributes = {
    id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: null,
      comment: null,
      primaryKey: true,
      field: "id",
      autoIncrement: true
    },
    username: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "用户名",
      primaryKey: false,
      field: "username",
      autoIncrement: false
    },
    nickname: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "昵称",
      primaryKey: false,
      field: "nickname",
      autoIncrement: false
    },
    gender: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
      comment: "性别((1-男 2-女 0-保密)",
      primaryKey: false,
      field: "gender",
      autoIncrement: false
    },
    password: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "密码",
      primaryKey: false,
      field: "password",
      autoIncrement: false
    },
    deptId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
      comment: "部门ID",
      primaryKey: false,
      field: "dept_id",
      autoIncrement: false
    },
    avatar: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "用户头像",
      primaryKey: false,
      field: "avatar",
      autoIncrement: false
    },
    mobile: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "联系方式",
      primaryKey: false,
      field: "mobile",
      autoIncrement: false
    },
    status: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
      comment: "状态((1-正常 0-禁用)",
      primaryKey: false,
      field: "status",
      autoIncrement: false
    },
    email: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "用户邮箱",
      primaryKey: false,
      field: "email",
      autoIncrement: false
    },
    createTime: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: null,
      comment: "创建时间",
      primaryKey: false,
      field: "create_time",
      autoIncrement: false,
      get() {
        const rawValue = this.getDataValue('createTime');
        return rawValue ? DateUtils.formatDate(rawValue, 'YYYY-MM-DD HH:mm:ss') : null;
      }
    },
    createBy: {
      type: DataTypes.BIGINT,
      allowNull: true,
      defaultValue: null,
      comment: "创建人ID",
      primaryKey: false,
      field: "create_by",
      autoIncrement: false
    },
    updateTime: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: null,
      comment: "更新时间",
      primaryKey: false,
      field: "update_time",
      autoIncrement: false,
      get() {
        const rawValue = this.getDataValue('updateTime');
        return rawValue ? DateUtils.formatDate(rawValue, 'YYYY-MM-DD HH:mm:ss') : null;
      }
    },
    updateBy: {
      type: DataTypes.BIGINT,
      allowNull: true,
      defaultValue: null,
      comment: "修改人ID",
      primaryKey: false,
      field: "update_by",
      autoIncrement: false
    },
    isDeleted: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
      comment: "逻辑删除标识(0-未删除 1-已删除)",
      primaryKey: false,
      field: "is_deleted",
      autoIncrement: false
    },
    openid: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "微信 openid",
      primaryKey: false,
      field: "openid",
      autoIncrement: false
    }
  };
  const options = {
    tableName: "sys_user",
    comment: "",
    indexes: []
  };
  const SysUserModel = sequelize.define("sys_user_model", attributes, options);

  // 添加关联关系
  SysUserModel.associate = function(models) {
    SysUserModel.hasMany(models.sys_user_role_model, {
      foreignKey: 'user_id'
    });
    SysUserModel.belongsToMany(models.sys_role_model, {
      through: models.sys_user_role_model,
      foreignKey: 'user_id',
      otherKey: 'role_id'
    });
    // 添加与部门的关联关系
    SysUserModel.belongsTo(models.sys_dept_model, {
      foreignKey: 'dept_id',
      targetKey: 'id',
      as: 'dept'
    });
  };

  return SysUserModel;
};